<?php

class EmbeddedPhoneNumberValidatorSchema extends sfValidatorSchema {
  
  protected function configure($options = array(), $messages = array()) {
    $this->addMessage ( 'area_code', 'Area code is required' );
    $this->addMessage ( 'call_through', 'Call through is required' );
  }
  
  protected function doClean($values) {
    $errorSchema = new sfValidatorErrorSchema ( $this );
    foreach ( $values as $key => $value ) {
      $localErrorSchema = new sfValidatorErrorSchema ( $this );
    
      
      // neither the one nor the other
      if (! $value ['area_code'] && ! $value ['call_through']) {
        unset ( $values [$key] );
      }
      
      // dialing code but no call through
      if ($value ['area_code'] && ! $value ['call_through']) {
        $localErrorSchema->addError ( new sfValidatorError ( $this, 'required' ), 'dialing_code' );
      }
      
      // call through but no dialing code
      if ($value ['call_through'] && ! $value ['area_code']) {
        $localErrorSchema->addError ( new sfValidatorError ( $this, 'required' ), 'call_through' );
      }
      
      if (count ( $localErrorSchema )) {
        $errorSchema->addError ( $localErrorSchema, ( string ) $key );
      }
    }
    
    if (count ( $errorSchema )) {
      throw new sfValidatorErrorSchema ( $this, $errorSchema );
    }
    
    return $values;
  }
}